import axios from 'axios';
import { Base64 } from 'js-base64';
import { showToast } from 'vant';
import emitter from './event.js'
const http = axios.create({
  baseURL: 'https://travelapi.teachlee.com/apif', // 设置请求的基础
  timeout: 30000, // 设置请求超时时间
});

// 对token加密
function encryptToken(){
	const orderid = ''//订单号
	const {user_Token} = JSON.parse(localStorage.getItem('userInfor'))//从本地缓存里取出token
	const auth = Base64.encode(user_Token + ':' + orderid)//对token加密
	return 'Basic ' + auth
}
// 响应拦截器：是在axios请求发出之前
http.interceptors.request.use((config) => {
	// console.log(config.url);
	// if(config.url.startsWith('/ip')){
	// 	config.baseURL = LBS_URL;
	// }else{}
	// 设置请求头
	const token = localStorage.getItem('userInfor')
	if(token){
		config.headers.Authorization = encryptToken()
	}
  return config;
}, (error) => {
  console.log(error)
  return Promise.reject(error);
});

// 响应拦截器：是在axios请求发出之后
http.interceptors.response.use((response) => {
  // 处理响应数据
  // ...

  return response.data;
}, (error) => {
  console.log(error)
  // 处理响应错误
  if(error.response){
	  const status =  error.response.status
	  switch (status){
		case 404:
		// console.error('接口不存在')
		break;
		case 401:
		emitter.emit('login-pop-up');
		// console.error('没有权限')
		break;
		case 500:
		showToast({message:'发生异常错误',duration:800});
		break;
		case 501:
		showToast({message:'发生异常错误',duration:800});
		break;
		case 502:
		showToast({message:'发生异常错误',duration:800});
		break;
		case 400:
		// console.error(error.response.data.msg)
		break;
		case 422:
		showToast({message:error.response.data.msg,duration:1000});
		break;
	  }
  }else if(error.message == "timeout of 30000ms exceeded"){
	  showToast({message:'请求超时,检查网络环境',duration:800});
  }
  return Promise.reject(error);
});

export default {
  get(url, params) {
    return http.get(url, { params });
  },

  post(url, data) {
    return http.post(url, data);
  },
};